package fuzion24.device.vulnerability.test;


import android.net.Uri;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ShareViaGoogleForm {
    private static final String mFormUrl;
    private static Map<String, String> submitMapping = new HashMap<>();
    private final JSONObject mCombinedResults;

    static {
        mFormUrl = "https://docs.google.com/forms/d/1hAoRFCmPmIEzKD_gQBZhne2Jkr24E5VTuI_yGXKnVyE/formResponse";

        submitMapping.put("fingerprint", "entry.957780611");
        submitMapping.put("kernelVersion", "entry.173229528");
        submitMapping.put("brand", "entry.1413588413");
        submitMapping.put("manufacturer", "entry.2048857005");
        submitMapping.put("model", "entry.2054096086");
        submitMapping.put("release", "entry.1474130928");
        submitMapping.put("sdk", "entry.1701379105");
        submitMapping.put("versionCode", "entry.275175001");
        submitMapping.put("versionName", "entry.506488905");
        submitMapping.put("id", "entry.218715471");
        submitMapping.put("combinedResults", "entry.2112177772");

    }

    public ShareViaGoogleForm(JSONObject combinedResults) {
        mCombinedResults = combinedResults;
    }

    public Uri buildUri() throws JSONException {
        JSONObject buildInfo = mCombinedResults.getJSONObject("buildInfo");
        Uri.Builder builder = Uri.parse(mFormUrl).buildUpon();

        Iterator<String> keys = buildInfo.keys();
        while (keys.hasNext()) {
            String curKey = keys.next();
            if (!submitMapping.containsKey(curKey)) {
                continue;
            }

            String result = buildInfo.getString(curKey);
            String formKey = submitMapping.get(curKey);
            builder.appendQueryParameter(formKey, result);
        }

        // lastly put the JSON string as the combinedResults form value
        String combinedResultsFormKey = submitMapping.get("combinedResults");
        String results = mCombinedResults.toString();
        builder.appendQueryParameter(combinedResultsFormKey, results);

        return builder.build();
    }
}
